package com.aaa.member.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.entity.MemberLevel;
import com.aaa.member.service.MemberLevelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 会员等级表(MemberLevel)表控制层
 *
 * @author makejava
 * @since 2020-11-21 16:50:41
 */
@RestController
@RequestMapping("memberLevel")
public class MemberLevelController {
    /**
     * 服务对象
     */
    @Resource
    private MemberLevelService memberLevelService;

    /**
     * 查找全部会员等级
     * @param memberLevel
     * @return
     */
    @GetMapping("queryMemberLevel")
    public Result queryMemberLevel(Integer pageNo,Integer pageSize,MemberLevel memberLevel){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return memberLevelService.queryMemberLevel(pageNo,pageSize,memberLevel);
    }
    /**
     * 修改会员等级
     * @param memberLevel
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody MemberLevel memberLevel){
        //当ID空为时抛出自定义异常
        if (memberLevel.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.memberLevelService.update(memberLevel));
    }
    /**
     * 添加会员等级
     * @param memberLevel
     * @return
     */
    @PostMapping("insert")
    public Result inset(@RequestBody MemberLevel memberLevel) {
        //判断新增的角色名字为空抛出异常
        if (memberLevel.getName() == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        } else {
            //返回成功
            return new Result(ReturnCode.SUCCESSS.getCode(),
                    ReturnCode.SUCCESSS.getMsg(), this.memberLevelService.insert(memberLevel));
        }
    }

    /**
     * 删除会员等级
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id){
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.memberLevelService.deleteById(id));
    }

}